/*
@title: Puzzle Run
@description: Puzzle Run is a puzzle game where you navigate through mazes while pushing a rocks onto detectors to unlock areas and reach the goal.
@author: Biffot
@tags: []
@addedOn: 2024-11-24
*/
const player = "p";
const wall = "w";
const wall2 = "2";
const wallr = "3";
const walll = "4";
const wallt = "5";
const wallb = "6";
const wallc = "7";
const wallc2 = "8";
const wallc3 = "9";
const wallc4 = "0";
const coin = "c";
const rock = "r";
const end = "e";
const blockage = "b";
const detector = "d";
var gameOver = false;
var coins = 0;
setLegend(
  [ player, bitmap`
....00000000....
...0666666660...
..066666666660..
.06666666666660.
0666006666006660
0666006666006660
0666666666666660
0666666666666660
0666666666666660
0666666666666660
0660666666660660
0666066666606660
.06660000006660.
..066666666660..
...0666666660...
....00000000....` ],
  [ wall, bitmap`
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100`],
  [ wall2, bitmap`
0000000000000000
0000000000000000
1111111111111111
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
1111111111111111
0000000000000000
0000000000000000`],
  [ wallr, bitmap`
0000000000000000
0000000000000000
1111111111111100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
1111111111111100
0000000000000000
0000000000000000`],
  [ walll, bitmap`
0000000000000000
0000000000000000
0011111111111111
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0011111111111111
0000000000000000
0000000000000000`],
  [ wallt, bitmap`
0000000000000000
0000000000000000
0011111111111100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100`],
  [ wallb, bitmap`
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0010000000000100
0011111111111100
0000000000000000
0000000000000000`],
  [ wallc, bitmap`
0010000000000100
0010000000000100
1110000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
1111111111111100
0000000000000000
0000000000000000`],
  [ wallc2, bitmap`
0010000000000100
0010000000000100
0010000000000111
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0011111111111111
0000000000000000
0000000000000000`],
  [ wallc3, bitmap`
0000000000000000
0000000000000000
1111111111111100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
0000000000000100
1110000000000100
0010000000000100
0010000000000100`],
  [ wallc4, bitmap`
0000000000000000
0000000000000000
0011111111111111
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000000
0010000000000111
0010000000000100
0010000000000100`],
  [ coin, bitmap`
....DDDDDDDD....
...D22222222D...
..D2244444422D..
.D224444444422D.
D224444DD444422D
D24444D4D444442D
D2444D44D444442D
D2444444D444442D
D2444444D444442D
D2444444D444442D
D2444444D444442D
D22444DDDDD4422D
.D224444444422D.
..D2244444422D..
...D22222222D...
....DDDDDDDD....`],
  [ rock, bitmap`
2222222222222222
2222222222222222
2222222222222222
222222LLLLLL2222
2222LLL1111LL222
222LL1111111LL22
22LL1111L1111LLL
2LL11LLL1111111L
2L111L1111111111
LL11111111111111
1111111LLLL11111
111111L111111111
111111111111LLLL
11LLLL111111111L
1111111111111111
1111111111111111`],
  [ end, bitmap`
0022002200220022
0022002200220022
2200220022002200
2200220022002200
0022002200220022
0022002200220022
2200220022002200
2200220022002200
0022002200220022
0022002200220022
2200220022002200
2200220022002200
0022002200220022
0022002200220022
2200220022002200
2200220022002200`],
  [ blockage, bitmap`
3311111111111133
333..........333
1333........3331
1.333......333.1
1..333....333..1
1...333..333...1
1....333333....1
1.....3333.....1
1.....3333.....1
1....333333....1
1...333..333...1
1..333....333..1
1.333......333.1
1333........3331
333..........333
3311111111111133`],
  [ detector, bitmap`
....77777777....
...7777221777...
..777772217777..
.77777722177777.
7777777221777777
7777777221777777
7777777221777777
7777777221777777
7777777221777777
7777777221777777
7777222222221777
7777222222221777
.77772222221777.
..777722221777..
...7777221777...
....77777777....`]
);

setSolids([ player, wall, wall2, wallr, walll, wallt, wallb, rock, blockage]);

let level = 1;
const levels = [
  map`
..........
..........
..........
..........
....pc....
..........
..........
..........`,
  map`
c.....5eee
......w...
......w...
......w...
......6rrr
..........
.p........
..........`,
  map`
p.........
..........
..........
42222r2223
......b..c
.4222.2223
.........e
42222d2223`,
  map`
4229e.....
pbcw42229b
.027..5dw.
.w....6.w.
.w.r....w.
.83.423.6.
..........
4222222223`,
  map`
..029ec...
p.wdwe....
..6.829bbb
.....dw...
r.5.427.r.
..6.......
......5.r.
....r.6...`,
  map`
cb42222229
bb.....5dw
d....r.6.w
.........w
42295r43.w
p..w6....w
bb.82223.w
eb.......6`,
];

setMap(levels[level]);
refreshUI();
setPushables({
  [ player ]: [rock],
  [ rock ]: [rock]
  
});
function refreshUI(){
  clearText()
  addText(`Coins: ${coins}`,{x:0})
}
function movePlayer(dx, dy) {
  if (gameOver) return; 

  const playerTile = getFirst(player);
  const newX = playerTile.x + dx;
  const newY = playerTile.y + dy;
  playerTile.x = newX;
  playerTile.y = newY;
  if (tilesWith(player, coin).length > 0) {
    coins++
    coinTile = getFirst(coin)
    coinTile.remove()
    refreshUI()
  }
  if (level <4 ){
    if (tilesWith(rock, detector).length > 0) {
      detectorTile = getFirst(detector)
      detectorTile.remove()
      blockTiles = getAll(blockage)
      for (let i = 0; i < blockTiles.length; i++) {
        blockTile = blockTiles[i]
        blockTile.remove()
      }
    }
  }
  if (level >= 4 ){
    if (tilesWith(rock, detector).length > 1) {
      detectorTiles = getAll(detector)
      for (let i = 0; i < detectorTiles.length; i++) {
        detectorTile = detectorTiles[i]
        detectorTile.remove()
      }
      
      blockTiles = getAll(blockage)
      for (let i = 0; i < blockTiles.length; i++) {
        blockTile = blockTiles[i]
        blockTile.remove()
      }
    }
  }
  if (tilesWith(player, end).length > 0) {
    if (coins > level) {
      coins = level;
    }
    level++
    refreshUI();
    if (level < levels.length) {
      setMap(levels[level]);
    } else {
      addText("You Win!", { y: 4, color: color`4` });
      addText(`Coins Collected: ${coins}/5`, { y: 5, color: color`7` });
      gameOver = true; 
      setMap(levels[0])
    }
  }
}
function resetGame(){
  setMap(levels[1]);
  coins=0;
  refreshUI();
}
function resetLevel(){
  setMap(levels[level])
  if (coins > level) {
    coins = level;
  }
  refreshUI();
}
onInput("w", () => movePlayer(0, -1));
onInput("s", () => movePlayer(0, 1));
onInput("a", () => movePlayer(-1, 0));
onInput("d", () => movePlayer(1, 0));
onInput("j", () => resetLevel());
onInput("i", () => resetGame());
